package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.task.ArticleHtmlTasks;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author xunyuhua
 * @since 2022-06-30
 */
@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private ArticleHtmlTasks articleHtmlTasks;

    /**
     * 保存文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        if (dto.getId() != null) {
            //修改操作
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            this.updateById(apArticle);

            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.eq(ApArticleContent::getArticleId, apArticle.getId());
            updateWrapper.set(ApArticleContent::getContent, dto.getContent());
            apArticleContentService.update(updateWrapper);

            //生成静态页面
            articleHtmlTasks.createHtml(apArticle,dto.getContent());

            return ResponseResult.okResult(apArticle.getId());
        } else {
            //新增操作
            //需要保存两张表的数据
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setCreatedTime(new Date());

            apArticle.setId(null);
            this.save(apArticle);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setId(null);
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);

            //生成静态页面
            articleHtmlTasks.createHtml(apArticle,dto.getContent());


            return ResponseResult.okResult(apArticle.getId());

        }

    }

    /**
     * app端首页展示文章
     *
     * @param dto
     * @param type 0代表初次加载或加载更新,1代表加载更多
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件：频道id  0--查询所有频道
        if (dto.getChannelId() != 0) {
            queryWrapper.eq(ApArticle::getChannelId, dto.getChannelId());
        }
        if (type == 0) {
            queryWrapper.gt(ApArticle::getPublishTime, dto.getMaxTime());
        } else {
            queryWrapper.lt(ApArticle::getPublishTime, dto.getMaxTime());
        }

        //隐藏了两个条件  是否下架  是否删除
        queryWrapper.eq(ApArticle::getIsDelete, false);
        queryWrapper.eq(ApArticle::getIsDown, false);

        //发布时间排序
        queryWrapper.orderByDesc(ApArticle::getPublishTime);

        //分页
        Page<ApArticle> page = new Page<>(1, dto.getSize());
        Page<ApArticle> page1 = this.page(page, queryWrapper);
        return ResponseResult.okResult(page1.getRecords());

    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        //查询两个表数据
        //根据文章id查询ap_article表
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle == null) {
            log.error("查不到数据，文章id是：" + dto.getArticleId());
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据文章id查询ap_article_content表
        LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent apArticleContent = apArticleContentService.getOne(queryWrapper);

        Map resultMap = new HashMap();
        resultMap.put("config",apArticle);
        resultMap.put("content",apArticleContent);

        return ResponseResult.okResult(resultMap);
    }
}
